﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.DocumentPrinter;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.BLL.TaxonomyTerms;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.DocumentPrinter
{
    public class DocumentDeliveryConfigPresenter : BasePresenter<IDocumentDeliveryConfigView>
    {
        public DocumentDeliveryConfigPresenter(IDocumentDeliveryConfigView view) : base(view) { }

        public void HandleLoad()
        {
            Radicado foundEntity = DocumentDeliveryConfigBLL.GetDocument(View.DocumentID, View.LibraryName);
            //View.RadID = foundEntity.NoRadicado;
            View.SelectedTerm = foundEntity.Countries;
            View.DocumentType = foundEntity.GetType().Name;
            View.DeliveryMeans = DocumentDeliveryConfigBLL.GetDeliveryMeans();

            TaxonomyTerms bllObject = new TaxonomyTerms();
            View.Countries = bllObject.GetCountries();
            if (View.SelectedTerm != null)
            {
                Microsoft.SharePoint.Taxonomy.Term selectedState = TermStoreHelper.GetParentTerm(View.SelectedTerm,
                    GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_COUNTRY);
                Microsoft.SharePoint.Taxonomy.Term selectedCity = TermStoreHelper.GetTermByTaxonomyValue(View.SelectedTerm,
                    GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_COUNTRY);
                Microsoft.SharePoint.Taxonomy.Term selectedCountry = selectedState.Parent;
                View.Cities = selectedState.Terms;
                View.States = selectedCountry.Terms;
                View.SelectedCountry = selectedCountry.Labels[0].Value;
                View.SelectedState = selectedState.Labels[0].Value;
            }
            View.Show();

            if (View.DocumentType.Equals("RadicadoEmpresaSalida"))
            {
                RadicadoEmpresaSalida cFoundEntity = foundEntity as RadicadoEmpresaSalida;
                View.ToEmail = cFoundEntity.Email;
                View.ToFirstNameOrLegalReason = cFoundEntity.RazónSocial;
                View.ToLastNameOrInitials = cFoundEntity.Sigla;
                View.ToSecondLastNameOrCEO = cFoundEntity.RepresentanteLegal;
                View.ToPhoneNumber = cFoundEntity.Teléfono;
                View.Address = cFoundEntity.Dirección;
            }
            if (View.DocumentType.Equals("RadicadoPersonaSalida"))
            {
                RadicadoPersonaSalida pFoundEntity = foundEntity as RadicadoPersonaSalida;
                View.ToEmail = pFoundEntity.Email;
                View.ToFirstNameOrLegalReason = pFoundEntity.Nombres;
                View.ToLastNameOrInitials = pFoundEntity.PrimerApellido;
                View.ToSecondLastNameOrCEO = pFoundEntity.SegundoApellido;
                View.ToPhoneNumber = pFoundEntity.Teléfono;
                View.Address = pFoundEntity.Dirección;

            }

            View.ChangeLabels();


        }


        public void LoadStates()
        {
            View.States = new TaxonomyTerms().GetStates(View.SelectedCountry);
            View.ShowStates();
        }

        public void LoadCities()
        {
            View.Cities = new TaxonomyTerms().GetCities(View.SelectedCountry, View.SelectedState);
            View.ShowCities();
        }

        public void SendDeliveryMail()
        {
           new  DocumentDeliveryConfigBLL().UpdateSendMailDelivery(View.DocumentID, View.LibraryName, View.SelectedTerm, View.Address, View.ToEmail, View.ToPhoneNumber);
           View.ShowMessage("Datos actualizados exitosamente");
        }
    }
}
